﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmThanhToan

    Private _ThanhTien As Decimal
    Public Property ThanhTien() As Decimal
        Get
            Return _ThanhTien
        End Get
        Set(ByVal value As Decimal)
            _ThanhTien = value
        End Set
    End Property

    Private _TienKHTra As Decimal
    Public Property TienKHTra() As Decimal
        Get
            Return _TienKHTra
        End Get
        Set(ByVal value As Decimal)
            _TienKHTra = value
        End Set
    End Property

    Private _TienTraLai As Decimal
    Public Property TienTraLai() As Decimal
        Get
            Return _TienTraLai
        End Get
        Set(ByVal value As Decimal)
            _TienTraLai = value
        End Set
    End Property

    Dim bAltrF4 As Boolean = False

    Private Function GetValues(ByVal obj As Object) As Decimal
        If obj IsNot Nothing AndAlso IsNumeric(obj) Then
            Return Convert.ToDecimal(obj)
        Else
            Return 0
        End If
    End Function

    Private Function CheckSave() As String
        If (txtTienKHTra.EditValue Is Nothing OrElse txtTienKHTra.Text.Trim = "" Or txtTienKHTra.Text.Trim = "0") Then txtTienKHTra.EditValue = txtThanhTien.EditValue
        If txtTienKHTra.Text.Trim = "" Then
            Return "Bạn chưa nhập tiền thanh toán"
        ElseIf GetValues(txtTienTraLai.EditValue) < 0 Then
            Return "Khách hàng không được trả thiếu"
        End If
        Return ""
    End Function

    Private Sub FrmThanhToan_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            Dim str = CheckSave()
            If str <> "" Then
                MsgErr(str)
                Return
            End If
            If IsNumeric(txtTienKHTra.EditValue) AndAlso CDec(txtTienKHTra.EditValue) > 0 Then
                TienKHTra = CDec(txtTienKHTra.EditValue)
                TienTraLai = CDec(txtTienTraLai.EditValue)
                bAltrF4 = False
                Me.Close()
            End If
        ElseIf e.Modifiers = Keys.Alt AndAlso e.KeyCode = Keys.F4 Then
            'TienKHTra = -1
            'TienTraLai = -1
            'Me.Close()
            bAltrF4 = True
        End If
    End Sub

    Private Sub FrmThanhToan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtThanhTien.EditValue = ThanhTien
    End Sub

    Private Sub txtTienKHTra_EditValueChanging(ByVal sender As System.Object, ByVal e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles txtTienKHTra.EditValueChanging
        e.Cancel = CheckSoAm(e.NewValue)
    End Sub

    Private Sub txtTienKHTra_Spin(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.SpinEventArgs) Handles txtTienKHTra.Spin
        e.Handled = True
    End Sub

    Private Sub txtTienKHTra_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTienKHTra.EditValueChanged
        txtTienTraLai.EditValue = GetValues(txtTienKHTra.EditValue) - ThanhTien
    End Sub

    Private Sub txtTienKHTra_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTienKHTra.KeyDown
        Try
            If (e.KeyCode = Keys.Add OrElse e.Modifiers = Keys.Shift And e.KeyCode = Keys.Oemplus) AndAlso txtTienKHTra.Text.Trim.Length > 0 AndAlso txtTienKHTra.Text.Trim.Length < 15 Then
                txtTienKHTra.EditValue += "000"
            ElseIf (e.KeyCode = Keys.Subtract OrElse (e.KeyCode = Keys.OemMinus AndAlso e.Modifiers <> Keys.Shift AndAlso e.Modifiers <> Keys.Control AndAlso e.Modifiers <> Keys.Alt)) AndAlso txtTienKHTra.Text.Trim.Length > 3 Then
                Dim count As Integer = 0
                Dim i As Integer = txtTienKHTra.Text.Trim.Length - 1
                While True
                    If count >= 3 OrElse i < 0 OrElse txtTienKHTra.Text.Trim.Chars(i).ToString <> "0" Then
                        Exit While
                    End If
                    i -= 1
                    count += 1
                End While
                txtTienKHTra.EditValue = txtTienKHTra.EditValue.ToString.Substring(0, txtTienKHTra.EditValue.ToString.Length - count)
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub FrmThanhToan_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If bAltrF4 Then
            e.Cancel = True
        End If
    End Sub
End Class